import Vue from 'vue'
import VueRouter from 'vue-router'

import store from './store'

import App from './App.vue'
import Login from 'pages/Login.vue'
import Index from 'pages/Index.vue'

import Exam from 'pages/Exam/Index.vue'
import Running from 'pages/Exam/Running.vue'
import History from 'pages/Exam/History.vue'
import New from 'pages/Exam/New/Index.vue'

const routes = [{
	name: 'app',
	path: '/',
    redirect: { name: 'index' },
	component: App,
  	children: [{ 
  		name: 'index',
  		path: 'index',
  		component: Index,
  	}, {
  		name: 'login',
		path: 'login', 
		component: Login,
  	}, {
        name: 'exam',
        path: 'exam',
        component: Exam,
        meta: { requiresAuth: true },
    }],
}];

Vue.use(VueRouter);

let router = new VueRouter({
    routes,
});

// logged judge
router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.requiresAuth)) {

        if (!store.getters['user/logged']) {
            next({
                name: 'login',
                query: { redirect: to.fullPath }
            })
        } else {
            next()
        }
    }
    else 
        next();
});

export default router;